﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FisheryPlatform.Search.Index;
using FisheryPlatform.Bussiness.Product;
using FisheryPlatform.Search.Entity;
using FisheryPlatform.Search.UnitTest;
using System.Diagnostics;

namespace FisheryPlatform.Web.WWW.Search
{
    public partial class Index : System.Web.UI.Page
    {
        protected ProductQueryResultEntity proQueryResult = new ProductQueryResultEntity();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            #region
            var text = "";
            //if (string.IsNullOrEmpty(Request.QueryString["reindex"]) == false && Request.QueryString["reindex"] == "1")
            {
                var pageCount = 0;
                var dataList = ProductManager.Search("1=1", 0, 0, out pageCount);
                var indexProList = new List<ProductIndexEntity>();

                dataList.ForEach(pro =>
                {
                    indexProList.Add(new ProductIndexEntity().Get(pro));
                    text += pro.Title + pro.Description;
                });
                //indexProList.AddRange(indexProList);
                IndexProduct indexPro = new IndexProduct();
                //indexPro.Analyze(indexProList);
                //var len = 10;
                //var updatePro = indexProList[len];
                //updatePro.Title = updatePro.Title + " test update";
                //indexPro.UpdateIndex(updatePro);
                //Response.Write(updatePro.ProductId);
            }
            #endregion

            IndexProduct index = new IndexProduct();

            if (string.IsNullOrEmpty(Request.QueryString["id"]) == false)
                index.DeleteIndex(Request.QueryString["id"].Trim());

            proQueryResult = index.Search(new ProductQueryEntity { Keyword = Request.QueryString["query"] });

            Stopwatch timer = new Stopwatch();

            //timer.Reset();
            //timer.Start();
            //Response.Write(AnalyzerTest.MM(text).ToString());
            //timer.Stop();
            //Response.Write("<br/>" + timer.ElapsedMilliseconds + "<br/>");

            //timer.Reset();
            //timer.Start();
            //Response.Write(AnalyzerTest.IK(text).ToString());
            //timer.Stop();
            //Response.Write("<br/>" + timer.ElapsedMilliseconds + "<br/>");

            //timer.Reset();
            //timer.Start();
            //Response.Write(AnalyzerTest.Standard(text).ToString());
            //timer.Stop();
            //Response.Write("<br/>" + timer.ElapsedMilliseconds + "<br/>");

            //timer.Reset();
            //timer.Start();
            //Response.Write(AnalyzerTest.PanGu(text).ToString());
            //timer.Stop();
            //Response.Write("<br/>" + timer.ElapsedMilliseconds + "<br/>");

            //var q = Request.QueryString["query"];
            //if (string.IsNullOrEmpty(q) == false)
            //{
            //    var result = FPSearcher.Search(new FisheryPlatform.Search.Indexing.FPSearcher.FPSearchCondition { Queries = q, CurrentPageIndex = 1, Paging = 100, Raw = string.IsNullOrEmpty(Request.QueryString["raw"]) ? false : true });
            //    Response.Write(String.Join("", result.List.ToArray()));
            //    Response.Write(String.Join("<br/>", result.Raw.ToArray()));
            //}
        }
    }
}